/// author stevenHu
///
/// created by hujintao
/// created at 2021/12/13
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:hk_base/adapter.dart';
import 'package:hk_base/hk_utils.dart';

class GeneralDialog extends StatefulWidget {
  ///点击弹窗外部，关闭弹窗，默认为true true：可以关闭 false：不可以关闭
  final bool outsideDismiss;

  /// 取消按钮回调
  final Function? cancelCallback;

  /// 确定按钮回调
  final Function? confirmCallBack;

  /// 高亮文本回调点击
  final Function? dismissCallback;

  /// 消失回调
  final Function? markCallback;

  /// 富文本前缀
  final String? prefixText;

  /// 富文本高亮文字
  final String? markText;

  /// 富文本后缀
  final String? suffixText;

  /// 标题文本
  final String? title;

  /// 消息文本，非富文本
  final String? message;

  /// 取消标题
  final String? cancelTitle;

  /// 确定标题
  final String? confirmTitle;

  /// message组件 设置该widget后 默认效果可自行定义
  final Widget? widgetForMessage;

  const GeneralDialog({
    Key? key,
    this.outsideDismiss = true,
    this.cancelCallback,
    this.confirmCallBack,
    this.dismissCallback,
    this.prefixText,
    this.markText,
    this.suffixText,
    this.message,
    this.cancelTitle,
    this.confirmTitle,
    this.title,
    this.markCallback,
    this.widgetForMessage = const SizedBox(),
  }) : super(key: key);

  @override
  _GeneralDialogState createState() => _GeneralDialogState();
}

class _GeneralDialogState extends State<GeneralDialog> {
  _cancelClick() {
    _dismissDialog();
    if (widget.cancelCallback != null) {
      widget.cancelCallback!();
    }
  }

  _confirmClick() {
    _dismissDialog();
    if (widget.confirmCallBack != null) {
      widget.confirmCallBack!();
    }
  }

  _dismissDialog() {
    if (widget.dismissCallback != null) {
      widget.dismissCallback!();
    }
    Navigator.of(context).pop();
  }

  _markClick() {
    _dismissDialog();
    if (widget.markCallback != null) {
      widget.markCallback!();
    }
  }

  final leftGradient = HkUtils.parseAngleToAlignment(57);
  final rightGradient = HkUtils.parseAngleToAlignment(45);

  @override
  Widget build(BuildContext context) {
    Color color = Theme.of(context).primaryColor;

    Column _columnWidget = Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            // 标题组件
            widget.title != null && widget.title!.isNotEmpty
                ? Container(
                    margin: EdgeInsets.only(top: Adapter.setWidth(30)),
                    child: Center(
                      child: Text(
                        widget.title ?? '',
                        style: TextStyle(
                          fontSize: Adapter.setSp(40),
                          color: const Color(0xff333333),
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  )
                : Container(),
            // 消息组件
            Container(
              margin: EdgeInsets.only(
                left: Adapter.setWidth(50),
                right: Adapter.setWidth(50),
                top: widget.title != null && widget.title!.isNotEmpty
                    ? Adapter.setHeight(22)
                    : Adapter.setHeight(90),
              ),
              child: widget.widgetForMessage ?? (widget.markText != null
                      ? RichText(
                          text: TextSpan(
                            text: widget.prefixText ?? '',
                            style: TextStyle(
                              fontSize: Adapter.setSp(28),
                              color: const Color(0xFF333333),
                            ),
                            children: <TextSpan>[
                              TextSpan(
                                text: widget.markText ?? '',
                                style: TextStyle(
                                  fontSize: Adapter.setSp(28),
                                  color: color,
                                ),
                                recognizer: TapGestureRecognizer()
                                  ..onTap = () => _markClick(),
                              ),
                              TextSpan(
                                text: widget.suffixText ?? '',
                                style: TextStyle(
                                  fontSize: Adapter.setSp(28),
                                  color: const Color(0xFF333333),
                                ),
                              ),
                            ],
                          ),
                        )
                      : Text(
                          widget.message ?? '',
                          style: TextStyle(
                            fontSize: Adapter.setSp(30),
                            color: const Color(0xFF333333),
                          ),
                        )),
            ),
            // 按钮组件
            widget.confirmTitle != null && widget.cancelTitle == ''
                ? GestureDetector(
                    onTap: _confirmClick,
                    child: Container(
                      margin: EdgeInsets.only(
                        left: Adapter.setWidth(90),
                        right: Adapter.setWidth(90),
                        top: Adapter.setWidth(43),
                        bottom: Adapter.setWidth(50),
                      ),
                      decoration: BoxDecoration(
                          color: color,
                          borderRadius: BorderRadius.circular(
                            Adapter.setHeight(100),
                          ),
                          boxShadow: [
                            BoxShadow(
                              color: Colors.black.withOpacity(0.16),
                              offset: Offset(0, Adapter.setHeight(3)),
                              spreadRadius: 1,
                              blurRadius: Adapter.setHeight(6),
                            )
                          ]),
                      height: Adapter.setWidth(90),
                      child: Center(
                        child: Text(
                          widget.confirmTitle ?? '继续',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: Adapter.setSp(30),
                          ),
                        ),
                      ),
                    ),
                  )
                : Container(
                    margin: EdgeInsets.only(
                      top: Adapter.setWidth(50),
                      bottom: Adapter.setWidth(50),
                    ),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        // 取消按钮
                        GestureDetector(
                          onTap: _cancelClick,
                          child: Container(
                            decoration: BoxDecoration(
                              color: color.withOpacity(0.3),
                              borderRadius:
                                  BorderRadius.circular(Adapter.setHeight(100)),
                            ),
                            width: Adapter.width * 0.31,
                            height: Adapter.setWidth(90),
                            child: Center(
                              child: Text(
                                widget.cancelTitle ?? '取消',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: Adapter.setSp(30),
                                ),
                              ),
                            ),
                          ),
                        ),
                        // 确定按钮
                        GestureDetector(
                          onTap: _confirmClick,
                          child: Container(
                            decoration: BoxDecoration(
                              color: color,
                              borderRadius: BorderRadius.circular(
                                Adapter.setHeight(100),
                              ),
                              boxShadow: [
                                BoxShadow(
                                  color: Colors.black.withOpacity(0.16),
                                  offset: Offset(0, Adapter.setHeight(3)),
                                  spreadRadius: 1,
                                  blurRadius: Adapter.setHeight(6),
                                )
                              ],
                            ),
                            width: Adapter.width * 0.31,
                            height: Adapter.setWidth(90),
                            child: Center(
                              child: Text(
                                widget.confirmTitle ?? '继续',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: Adapter.setSp(30),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
          ],
        ),
      ],
    );

    return WillPopScope(
        child: GestureDetector(
          onTap: () => {widget.outsideDismiss ? _dismissDialog() : null},
          child: Dialog(
              backgroundColor: Colors.transparent,
              elevation: 0,
              child: FractionallySizedBox(
                widthFactor: 1,
                child: Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius:
                          BorderRadius.circular(Adapter.setWidth(40))),
                  child: _columnWidget,
                ),
              )),
        ),
        onWillPop: () async {
          return widget.outsideDismiss;
        });
  }
}
